﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Data.Implements
{
    public class TinhThanhPhoDao : ITinhThanhPhoDao
    {
        public List<TinhThanhPho> GetAll()
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.TinhThanhPhos.OrderBy(x => x.Ten).ToList();
            }
        }

        public List<TinhThanhPho> GetTinhThanhPhos(int startIndex, int pageSize, string sorting, string name, out int totalRecords)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var query = from item in db.TinhThanhPhos
                            select item;

                // filter
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Ten.Contains(name));
                }

                totalRecords = query.Count();

                return pageSize > 0
                           ? query.OrderBy(sorting).Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public TinhThanhPho AddTinhThanhPho(TinhThanhPho tinhThanhPho)
        {
            using (var db = new NLKHCNDataEntities())
            {
                //NameIsExisted
                var count = (from o in db.TinhThanhPhos
                             where (o.Ten == tinhThanhPho.Ten)
                             select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.TinhThanhPhos.Add(tinhThanhPho);
                db.SaveChanges();
            }

            return tinhThanhPho;
        }

        public TinhThanhPho GetById(int id)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.TinhThanhPhos.FirstOrDefault(x => x.Id == id);
            }
        }

        public void UpdateTinhThanhPho(TinhThanhPho tinhThanhPho)
        {
            using (var db = new NLKHCNDataEntities())
            {
                //NameIsExisted
                var count = (from o in db.TinhThanhPhos
                             where (o.Ten == tinhThanhPho.Ten) && (o.Id != tinhThanhPho.Id)
                             select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.TinhThanhPhos.Attach(tinhThanhPho);
                db.Entry(tinhThanhPho).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void DeleteTinhThanhPho(int id)
        {
            if (id <= 0) return;
            using (var db = new NLKHCNDataEntities())
            {
                db.DeleteTinhThanhPho(id);
            }
        }
    }
}
